﻿package core {
	import flash.geom.Point;

    public class Bezier {

        private static var p0:Point;
        private static var p1:Point;
        private static var p2:Point;
        private static var step:uint;
        private static var ax:int;
        private static var ay:int;
        private static var bx:int;
        private static var by:int;
        private static var A:Number;
        private static var B:Number;
        private static var C:Number;
        private static var total_length:Number;

        private static function s(_arg1:Number):Number{
            return (Math.sqrt(((((A * _arg1) * _arg1) + (B * _arg1)) + C)));
        }
        private static function L(_arg1:Number):Number{
            var _local2:Number = Math.sqrt((C + (_arg1 * (B + (A * _arg1)))));
            var _local3:Number = ((((2 * A) * _arg1) * _local2) + (B * (_local2 - Math.sqrt(C))));
            var _local4:Number = Math.log((B + ((2 * Math.sqrt(A)) * Math.sqrt(C))));
            var _local5:Number = Math.log(((B + ((2 * A) * _arg1)) + ((2 * Math.sqrt(A)) * _local2)));
            var _local6:Number = ((2 * Math.sqrt(A)) * _local3);
            var _local7:Number = (((B * B) - ((4 * A) * C)) * (_local4 - _local5));
            return (((_local6 + _local7) / (8 * Math.pow(A, 1.5))));
        }
        private static function InvertL(_arg1:Number, _arg2:Number):Number{
            var _local4:Number;
            var _local3:Number = _arg1;
            do  {
                _local4 = (_local3 - ((L(_local3) - _arg2) / s(_local3)));
                if (Math.abs((_local3 - _local4)) < 1E-6){
                    break;
                };
                _local3 = _local4;
            } while (true);
            return (_local4);
        }
        public static function init(_arg1:Point, _arg2:Point, _arg3:Point, _arg4:Number):uint{
            p0 = _arg1;
            p1 = _arg2;
            p2 = _arg3;
            ax = ((p0.x - (2 * p1.x)) + p2.x);
            ay = ((p0.y - (2 * p1.y)) + p2.y);
            bx = ((2 * p1.x) - (2 * p0.x));
            by = ((2 * p1.y) - (2 * p0.y));
            A = (4 * ((ax * ax) + (ay * ay)));
            B = (4 * ((ax * bx) + (ay * by)));
            C = ((bx * bx) + (by * by));
            total_length = L(1);
            step = Math.floor((total_length / _arg4));
            if ((total_length % _arg4) > (_arg4 / 2)){
                step++;
            };
            return (step);
        }
		
        public static function getAnchorPoint(value:Number):Array{
            var _local2:Number;
            var _local3:Number;
            var _local4:Number;
            var _local5:Number;
            var _local6:Point;
            var _local7:Point;
            var _local8:Number;
            var _local9:Number;
            var _local10:Number;
            if ((((value >= 0)) && ((value <= step)))){
                _local2 = (value / step);
                _local3 = (_local2 * total_length);
                _local2 = InvertL(_local2, _local3);
                _local4 = (((((1 - _local2) * (1 - _local2)) * p0.x) + (((2 * (1 - _local2)) * _local2) * p1.x)) + ((_local2 * _local2) * p2.x));
                _local5 = (((((1 - _local2) * (1 - _local2)) * p0.y) + (((2 * (1 - _local2)) * _local2) * p1.y)) + ((_local2 * _local2) * p2.y));
                _local6 = new Point((((1 - _local2) * p0.x) + (_local2 * p1.x)), (((1 - _local2) * p0.y) + (_local2 * p1.y)));
                _local7 = new Point((((1 - _local2) * p1.x) + (_local2 * p2.x)), (((1 - _local2) * p1.y) + (_local2 * p2.y)));
                _local8 = (_local7.x - _local6.x);
                _local9 = (_local7.y - _local6.y);
                _local10 = Math.atan2(_local9, _local8);
                return (new Array(_local4, _local5, _local10));
            };
            return ([]);
        }

    }
}
